﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Aga.GestionDeArchivos.Domain.Entities;
using Aga.GestionDeArchivos.Services;
using Aga.GestionDeArchivos.Web.Models.Users;
using Aga.GestionDeArchivos.Web.Helpers;

namespace Aga.GestionDeArchivos.Web.Controllers
{
    public class UsersController : ControllerBase
    {
        private const int PageSize = 10;

        public UsersController()
        {
            ViewBag.Menu = MenuKey.Admin;
        }

        [CustomAuthorize(Actions = Actions.ViewUsers)]
        public ActionResult Index(int? index)
        {
            UserService service = new UserService();

            var users = service.GetAll();

            IndexModel model = new IndexModel
            {
                Users = service.GetPage(users, index ?? 0, PageSize)
            };

            return View(model);
        }

        [CustomAuthorize(Actions = Actions.CreateUser)]
        public ActionResult Create()
        {
            CreateModel model = new CreateModel();

            return View(model);
        } 

        [HttpPost]
        public ActionResult Create(CreateModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    UserService service = new UserService();

                    service.RegisterNewUser(
                        model.UserName,
                        model.Password,
                        model.ConfirmPassword,
                        model.Email,
                        model.ConfirmEmail,
                        model.FirstName,
                        model.LastName);

                    return RedirectToAction("Index");
                }
                else
                {
                    return View(model);
                }
            }
            catch (Exception ex)
            {
                return ShowError(ex);
            }
        }

        [CustomAuthorize(Actions = Actions.EditUser)]
        public ActionResult Edit(int id)
        {
            UserService service = new UserService();

            User user = service.GetById(id);

            EditModel model = new EditModel
            {
                Id = user.Id,
                FirstName = user.FirstName,
                LastName = user.LastName,
                Email = user.EMail,
                ConfirmEmail = user.EMail,
                UserName = user.Username,
                IsExternal = user.IsExternal
            };

            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(EditModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    UserService service = new UserService();

                    User user = service.GetById(model.Id);

                    user.Username = model.UserName;
                    user.EMail = model.Email;
                    user.FirstName = model.FirstName;
                    user.LastName = model.LastName;
                    user.IsExternal = model.IsExternal;

                    service.Save(user);

                    return RedirectToAction("Index");
                }
                else
                {
                    return View(model);
                }
            }
            catch (Exception ex)
            {
                return ShowError(ex);
            }
        }

        [CustomAuthorize(Actions = Actions.DeleteUser)]
        public ActionResult Delete(int id)
        {
            UserService service = new UserService();

            User user = service.GetById(id);

            DeleteModel model = new DeleteModel
            {
                Id = user.Id,
                FullName = user.FirstName + user.LastName
            };

            return View(model);
        }

        [HttpPost]
        public ActionResult Delete(DeleteModel model)
        {
            try
            {
                UserService service = new UserService();

                service.Delete(model.Id);

                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                return ShowError(ex);
            }
        }

        [CustomAuthorize(Actions = Actions.EditUser)]
        public ActionResult Roles(int id)
        {
            UserService users = new UserService();
            RoleService roles = new RoleService();

            User user = users.GetById(id);

            IEnumerable<Role> usersRoles = users.GetUsersRoles(id);

            IEnumerable<Role> allRoles = roles.GetAll();

            RolesModel model = new RolesModel
            {
                User = user,
                UsersRoles = usersRoles,
                AllRoles = allRoles
            };

            return View(model);

        }

        public ActionResult AddRole(int id, int roleId)
        {
            UserService users = new UserService();

            users.AddRoles(id, new int[] { roleId });

            return RedirectToAction("Roles", new { id = id });
        }

        public ActionResult RemoveRole(int id, int roleId)
        {
            UserService users = new UserService();

            users.RemoveRoles(id, new int[] { roleId });

            return RedirectToAction("Roles", new { id = id });
        }
    }
}
